package com.fesine.quartz.chapter3;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.triggers.SimpleTriggerImpl;

import java.util.Date;

/**
 * @description: 类描述
 * @author: Fesine
 * @createTime:2016/11/16 9:18
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2016/11/16 9:18
 */
public class Listing_3_6 {
    static Log logger = LogFactory.getLog(Listing_3_6.class);

    public static void main(String[] args) {
        Listing_3_6 example = new Listing_3_6();
        try {
            Scheduler scheduler = example.createScheduler();
            scheduler.start();
            example.schedulerJob(scheduler,ScanDirectoryJob.class,"sc01","d:/jsonFile/",-1,2000);
            example.schedulerJob(scheduler,ScanDirectoryJob.class,"sc02","d:",-1,2000);
            logger.info("调度器启动：" + new Date());
            Thread.sleep(10000);
            //scheduler.shutdown();
            //scheduler.pauseAll();
            scheduler.standby();
            Thread.sleep(5000);
            //scheduler.resumeAll();
            //scheduler.start();
        } catch (SchedulerException e) {
            logger.error(e);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * //通过调度器工厂创建调度器实例
     *
     * @return
     * @throws SchedulerException
     */
    public Scheduler createScheduler() throws SchedulerException {
        return StdSchedulerFactory.getDefaultScheduler();
    }

    /**
     * @param scheduler
     * @throws SchedulerException
     */
    private void schedulerJob(Scheduler scheduler, Class jobClass, String name, String path, int
            repeatCount, long repeatInterval) throws SchedulerException {
        //为job任务创建一个jobDetail
        //JobDetail jobDetail = new JobDetail();
        JobDetail jobDetail = JobBuilder.newJob(jobClass)
                .withIdentity(name, Scheduler.DEFAULT_GROUP)
                .withDescription("简单任务")
                .usingJobData("SCAN_DIR", path).build();
        //实现简单的触发器
        SimpleTrigger trigger = new SimpleTriggerImpl(name, repeatCount, repeatInterval);
        //将job和定时器在调度器中进行关联
        scheduler.scheduleJob(jobDetail, trigger);
    }

}
